package com.winit.project.util;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.util.ConfigUtil;
import com.winit.core.mq.util.MqMessageSendUtil;
import com.winit.project.core.bean.PackageTypeEnum;
import com.winit.project.task.BuildStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * project相关的ws消息发送工具类
 * Created by wangjian on 16/10/10.
 */
public class ProjectMessageUtil {

    private static Logger logger = LoggerFactory.getLogger(ProjectMessageUtil.class);

    /**
     * 向ws推送工程构建状态
     * @param queueProjectName 当前队列监听的工程名,构建jar工程时,为当前创建构建线程的war工程名称
     * @param projectName 当前构建的工程名
     * @param statusEnum 构建的状态
     */
    public static void projectBuildStatusMes(String queueProjectName,String projectName,BuildStatusEnum statusEnum){
        logger.info("com.winit.project.util.ProjectMessageUtil.projectBuildStatusMes queueProjectName="+queueProjectName
                +",projectName="+projectName+",statusEnum="+statusEnum.getDesc());

        String queueName = "project-"+queueProjectName+"-build";
        Map head = new HashMap();
        head.put("queueName",queueName);
        head.put("type","project_build_status");

        JSONObject object = new JSONObject();
        object.put("projectName",queueProjectName);
        object.put("showProjectName",projectName);
        object.put("status",statusEnum.getDesc());

        MqMessageSendUtil.sendMqMessage(queueName,head,object);
    }

    /**
     * 向ws推送工程构建日志
     * @param projectName 当前构建工程名 监听的队列工程
     * @param msg 消息
     */
    public static void projectBuildLogMes(String projectName,String msg){

        logger.info("com.winit.project.util.ProjectMessageUtil.projectBuildLogMes projectName="+projectName+",msg="+msg);
        if(StringUtils.isEmpty(projectName) || StringUtils.isEmpty(msg)){
            return;
        }

        String queueName = "project-"+projectName+"-build";
        Map head = new HashMap();
        head.put("queueName",queueName);
        head.put("type","project_build_log");

        JSONObject object = new JSONObject();
        object.put("projectName",projectName);
        object.put("message",msg);

        MqMessageSendUtil.sendMqMessage(queueName,head,object);
    }

    /**
     * sonar检查失败时,发送sonar访问地址至前台
     * @param projectName
     */
    public static void projectSonarMes(String projectName, PackageTypeEnum typeEnum){

        logger.info("com.winit.project.util.ProjectMessageUtil.projectSonarMes projectName="+projectName);
        if(StringUtils.isEmpty(projectName)){
            return;
        }

        String url = "";
        switch (typeEnum){
            case MAVEN_WAR:
                url = ConfigUtil.getValue("sonar_url");
                break;
            case PHP:
                url = ConfigUtil.getValue("php_sonar_url");
                break;
            default:
                break;
        }

        if(StringUtils.isEmpty(url)){
            logger.error("com.winit.project.util.ProjectMessageUtil.projectSonarMes url为空");
            return;
        }

        try {
            switch (typeEnum){
                case MAVEN_WAR:
                    url = url + "/dashboard?id="+ProjectBuildUtil.getSonarProjectKeyForWar(projectName)+"&did=1";
                    break;
                case PHP:
                    url = url + "/dashboard?id="+ProjectBuildUtil.getSonarProjectKeyForPhp(projectName)+"&did=1";
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            logger.error("com.winit.project.util.ProjectMessageUtil.projectSonarMes 异常.",e);
        }

        String queueName = "project-"+projectName+"-build";
        Map head = new HashMap();
        head.put("queueName",queueName);
        head.put("type","project_sonar_url");

        JSONObject object = new JSONObject();
        object.put("projectName",projectName);
        object.put("url",url);

        MqMessageSendUtil.sendMqMessage(queueName,head,object);
    }

    /**
     * 向ws推送工程跳板机推送状态
     * @param projectName
     * @param status
     * @param desc
     */
    public static void projectPushMsg(String projectName,String status,String desc){
        String queueName = "project-"+projectName+"-build";
        Map head = new HashMap();
        head.put("queueName",queueName);
        head.put("type","project_push_status");

        JSONObject object = new JSONObject();
        object.put("projectName",projectName);
        object.put("status",status);
        object.put("desc",desc);

        MqMessageSendUtil.sendMqMessage(queueName,head,object);
    }



}
